package com.zxpt.quetion.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 版权所有： 稻草人
 * 创建者： wenhe
 * 创建时间: 2020-02-18
 * 描述：解决请求跨域问题
 * 其它：
 **/
@Component
public class HeaderCorsFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        resp.setHeader("Access-Control-Allow-Origin", "*");
        resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        resp.setHeader("Access-Control-Max-Age", "3600");
        resp.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization");
        resp.setHeader("Access-Control-Allow-Credentials","true");
        filterChain.doFilter(servletRequest, resp);
    }
}
